package com.erp.shop.controller;

import cn.dev33.satoken.annotation.SaCheckPermission;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.erp.shop.domain.ErpSellerStoreCookie;
import com.erp.shop.domain.bo.ErpSellerStoreCookieBo;
import com.erp.shop.domain.vo.ErpSellerStoreCookieVo;
import com.erp.shop.mapper.ErpSellerStoreCookieMapper;
import com.erp.shop.service.IErpSellerStoreCookieService;
import com.ruoyi.common.core.annotation.RepeatSubmit;
import com.ruoyi.common.core.domain.PageQuery;
import com.ruoyi.common.core.domain.R;
import com.ruoyi.common.core.validate.AddGroup;
import com.ruoyi.common.core.validate.EditGroup;
import com.ruoyi.common.core.web.oldController.BaseController;
import com.ruoyi.common.core.web.oldPage.TableDataInfo;
import com.ruoyi.common.log.annotation.Log;
import com.ruoyi.common.log.enums.BusinessType;
import lombok.RequiredArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import javax.validation.constraints.NotEmpty;
import javax.validation.constraints.NotNull;
import java.util.Arrays;
import java.util.List;

/**
 * 店铺cookie信息保存
 *
 * @author erp
 * @date 2023-12-12
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/seller/sellerStoreCookie")
public class ErpSellerStoreCookieController extends BaseController {

    @Resource
    IErpSellerStoreCookieService iErpSellerStoreCookieService;
    private final ErpSellerStoreCookieMapper cookieMapper;

    /**
     * 获取cookie
     */
    @GetMapping("/getCookieByStoreId/{storeId}")
    public String getCookieByStoreId(@PathVariable("storeId") Long storeId) {
        ErpSellerStoreCookie storeCookie = cookieMapper.selectOne(new LambdaQueryWrapper<ErpSellerStoreCookie>().eq(ErpSellerStoreCookie::getStoreId, storeId).last("limit 1"));
        if (storeCookie == null) {
            return null;
        }
        return storeCookie.getCookie();
    }

    @GetMapping("/setCommission/{storeId}/{aeProductId}/{afflicateDiscount}")
    R<Object> setCommission(
            @PathVariable("storeId") Long storeId,
            @PathVariable("aeProductId") Long aeProductId,
            @PathVariable("afflicateDiscount") Integer afflicateDiscount) {
        return iErpSellerStoreCookieService.setCommission(storeId,aeProductId,afflicateDiscount);
    }

    /**
     * 获取ae店铺id
     */
    @GetMapping("/getChannelId/{storeId}")
    public String getChannelByStoreId(@PathVariable("storeId") Long storeId) {
        ErpSellerStoreCookie storeCookie = cookieMapper.selectOne(new LambdaQueryWrapper<ErpSellerStoreCookie>().eq(ErpSellerStoreCookie::getStoreId, storeId).last("limit 1"));
        if (storeCookie == null) {
            return null;
        }
        if (storeCookie.getChannelId() == null)
            return null;
        return storeCookie.getChannelId().toString();
    }

    /**
     * 查询店铺cookie信息保存列表
     */
    @GetMapping("/list")
    public TableDataInfo<ErpSellerStoreCookieVo> list(ErpSellerStoreCookieBo bo, PageQuery pageQuery) {
        return iErpSellerStoreCookieService.queryPageList(bo, pageQuery);
    }

    /**
     * 导出店铺cookie信息保存列表
     */
    @Log(title = "店铺cookie信息保存", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(ErpSellerStoreCookieBo bo, HttpServletResponse response) {
        List<ErpSellerStoreCookieVo> list = iErpSellerStoreCookieService.queryList(bo);
//        ExcelUtil.exportExcel(list, "店铺cookie信息保存", ErpSellerStoreCookieVo.class, response);
    }

    /**
     * 获取店铺cookie信息保存详细信息
     *
     * @param id 主键
     */
    @GetMapping("/{id}")
    public R<ErpSellerStoreCookieVo> getInfo(@NotNull(message = "主键不能为空")
                                             @PathVariable Long id) {
        return R.ok(iErpSellerStoreCookieService.queryById(id));
    }

    /**
     * 获取店铺cookie信息保存详细信息
     *
     * @param storeId 主键
     */
    @GetMapping("/getInfoByStoreId")
    public R<ErpSellerStoreCookieVo> getInfoByStoreId(Long storeId) {
        return iErpSellerStoreCookieService.getInfoByStoreId(storeId);
    }

    /**
     * 新增店铺cookie信息保存
     */
    @Log(title = "店铺cookie信息保存", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping()
    public R<Void> add(@Validated(AddGroup.class) @RequestBody ErpSellerStoreCookieBo bo) {
        return toAjax(iErpSellerStoreCookieService.insertByBo(bo));
    }

    /**
     * 修改店铺cookie信息保存
     */
    @Log(title = "店铺cookie信息保存", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping()
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody ErpSellerStoreCookieBo bo) {
        return toAjax(iErpSellerStoreCookieService.updateByBo(bo));
    }

    /**
     * 删除店铺cookie信息保存
     *
     * @param ids 主键串
     */
    @Log(title = "店铺cookie信息保存", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public R<Void> remove(@NotEmpty(message = "主键不能为空")
                          @PathVariable Long[] ids) {
        return toAjax(iErpSellerStoreCookieService.deleteWithValidByIds(Arrays.asList(ids), true));
    }
}
